package com.app.paginglearn.model

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.paging.*

import com.app.datamanagermodule.entity.User
import com.app.datamanagermodule.getUserDao
import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers


class PagingLearnModel : ViewModel() {

    private val TAG = PagingLearnModel::class.java.simpleName

    private val config: PagedList.Config = PagedList.Config.Builder()
            .setPageSize(10)
            //距离本页数据几个时候开始加载下一页数据(例如现在加载10个数据,设置prefetchDistance为2,则滑到第八个数据时候开始加载下一页数据).
            .setPrefetchDistance(8)//15
            //这里设置是否设置PagedList中的占位符,如果设置为true,我们的数据数量必须固定,由于网络数据数量不固定,所以设置false.
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(10)//15
            .build()

    private val pageDataSourceFactory = object : DataSource.Factory<Int, User>() {
        override fun create(): DataSource<Int, User> {

            return positionalDataSource
        }

    }

    val userList: LiveData<PagedList<User>> = getUserDao().selectAllUser().toLiveData(pageSize = 1)
    //variable must be initialized
    val userList2: LiveData<PagedList<User>> = LivePagedListBuilder(pageDataSourceFactory, config).build()



    val positionalDataSource = object : PositionalDataSource<User>() {

        override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<User>) {
            Log.d(TAG, "PositionalDataSource --> loadRange -> startPosition -> ${params.startPosition} -> loadSize -> ${params.loadSize}")
            getUserDao().selectUserByPosition(params.startPosition, params.loadSize)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : SingleObserver<MutableList<User>>{
                        override fun onSuccess(t: MutableList<User>) {
                            Log.d(TAG, "PositionalDataSource --> loadRange -> t -> $t")
                            callback.onResult(t)
                        }

                        override fun onSubscribe(d: Disposable) {

                        }

                        override fun onError(e: Throwable) {

                        }

                    })

        }

        override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<User>) {
            Log.d(TAG, "PositionalDataSource --> loadInitial -> requestedStartPosition -> ${params.requestedStartPosition} -> pageSize -> ${params.pageSize}")
            getUserDao().selectUserByPosition(params.requestedStartPosition, params.pageSize)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : SingleObserver<MutableList<User>>{
                        override fun onSuccess(t: MutableList<User>) {
                            Log.d(TAG, "PositionalDataSource --> loadInitial -> t -> $t")
                            callback.onResult(t, 8)
                        }

                        override fun onSubscribe(d: Disposable) {

                        }

                        override fun onError(e: Throwable) {

                        }

                    })

        }

    }
}
